package com.his.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.his.api.service.RegistrationService;
import com.his.dao.domain.Do.SelectAllRegistrationDo;
import com.his.dao.domain.dto.ListDto;
import com.his.dao.domain.dto.RegistrationDto;
import com.his.dao.domain.entity.Patient;
import com.his.dao.domain.entity.Registration;
import com.his.dao.domain.vo.ListVo;
import com.his.dao.mapper.RegistrationMapper;
import com.his.util.constant.RedisConstant;
import com.his.util.result.RestResult;
import com.his.util.result.RestResultBuilder;
import com.his.util.util.MapUtil;
import com.his.util.util.QrCodeUtil;
import com.his.util.util.StringUtil;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.LinkedHashMap;

import static com.his.util.constant.RedisConstant.PATIENT;
import static com.his.util.constant.RedisConstant.getPatientAndAuthorization;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author fall
 * @since 2022-11-28
 */
@Service
@AllArgsConstructor
public class RegistrationServiceImpl extends ServiceImpl<RegistrationMapper, Registration> implements RegistrationService {
    private RegistrationMapper registrationMapper;
    private RedisTemplate redisTemplate;

    @Override
    public RestResult all(String authorization, ListDto listDto) {
        Integer userId = StringUtil.getId(PATIENT, (String) redisTemplate.opsForValue().get(getPatientAndAuthorization(authorization)));

        return new RestResultBuilder().success(ListVo
                .builder()
                .list(registrationMapper.selectPatientAll(
                        userId,
                        listDto.getPage() * listDto.getPageSize(),
                        listDto.getPageSize()))
                .build());
    }

    @Override
    public RestResult selectById(Registration registration) {
        return new RestResultBuilder().success(registrationMapper.selectRegistrationDetails(registration.getRegistrationId()));
    }

    @Override
    public RestResult getCode(Registration registration) {
        return new RestResultBuilder().success(QrCodeUtil.generateQrCode(
                "http://192.168.0.9:8081/his/api/registration/payment?registrationId=" + registration.getRegistrationId(),
                "png", 400, 400));
    }

    @Override
    public String payment(Integer registrationId) {
        UpdateWrapper<Registration> updateWrapper = new UpdateWrapper<Registration>()
                .eq("registration_id", registrationId)
                .set("is_payment", 1);
        update(updateWrapper);
        return "<h1 style='text-align: center'> 支付完成 </h1>" +
                "<a style='text-align: center; font-size: 40px' href='http://192.168.0.9:8080/RegistrationDetails?registrationId=" + registrationId + "'> 返回 </a>";
    }

    @Override
    public RestResult atHospital(String authorization, Registration registration) {
        Integer id = StringUtil.getId(PATIENT, (String) redisTemplate.opsForValue().get(getPatientAndAuthorization(authorization)));
        registration.setIsAppointment(0);
        registration.setPatientId(id);
        updateById(registration);
        RestTemplate template = new RestTemplate();
        String url = "http://192.168.0.9:8082/his/admin/registration/registration";
        RestResult result = template.postForObject(url, registration, RestResult.class);

        return result;
    }

    @Override
    public RestResult history(String authorization, ListDto listDto) {
        Integer userId = StringUtil.getId(PATIENT, (String) redisTemplate.opsForValue().get(getPatientAndAuthorization(authorization)));

        return new RestResultBuilder().success(ListVo
                .builder()
                .list(registrationMapper.selectHistoryAll(
                        userId,
                        listDto.getPage() * listDto.getPageSize(),
                        listDto.getPageSize()))
                .build());
    }
}
